<template>
	<view style="padding:20rpx 30rpx;">
		<view class="card_r" v-for="(item, index) in list" :key="index">
			<view class="left">
				<view>{{item.shopName}}</view>
				<view>{{item.writeOffTime | formatDate}}</view>
			</view>
			<view class="right">
				线下核销
			</view>
		</view>
		
		<!-- 加载更多提示 -->
		<view class="load-more" v-if="loading">
			<view class="loading-dot"></view>
			<view class="loading-dot"></view>
			<view class="loading-dot"></view>
			<text>加载更多...</text>
		</view>
		
		<!-- 没有更多数据 -->
		<view class="no-more-data" v-if="!loading && !hasMore">
			<text>没有更多数据了</text>
		</view>
	</view>
</template>

<script>
import { writeOffpage } from '@/core/api/index.js'
export default {
	data() {
		return {
			total: 0,
			list: [],
			currentPage: 1,
			pageSize: 10,
			hasMore: true,
			loading: false
		}
	},
	onLoad() {
		this.fetchWriteOffRecords()
	},
	onReachBottom() {
		// 页面触底时加载更多
		if (!this.loading && this.hasMore) {
			this.loadMoreRecords()
		}
	},
	methods: {
		async fetchWriteOffRecords(isLoadMore = false) {
			if (this.loading) return
			
			this.loading = true
			
			if (!isLoadMore) {
				// 首次加载或重置
				this.currentPage = 1
				this.list = []
			} else {
				// 加载下一页
				this.currentPage++
			}
			
			try {
				const params = {
					page: this.currentPage,
					size: this.pageSize,
					condition: {
						type: 3,
						source: 1
					}
				}
				
				const { data } = await writeOffpage(params)
				console.log('获取核销记录:', data)
				
				this.total = data.total || 0
				
				if (isLoadMore) {
					// 追加数据
					this.list = [...this.list, ...data.records]
				} else {
					// 重置数据
					this.list = data.records
				}
				
				// 判断是否还有更多数据
				this.hasMore = this.list.length < this.total
				
			} catch (error) {
				console.error('获取核销记录失败:', error)
				if (isLoadMore && this.currentPage > 1) {
					// 请求失败时回退页码
					this.currentPage--
				}
			} finally {
				this.loading = false
			}
		},
		
		loadMoreRecords() {
			this.fetchWriteOffRecords(true)
		}
	}
}
</script>

<style lang="scss">
.card_r {
	display: flex;
	background-color: #fff;
	border-radius: 10rpx;
	justify-content: space-between;
	align-items: center;
	padding: 30rpx;
	margin-bottom: 20rpx;
	
	.left {
		view:nth-child(2) {
			color: #A3A3A3;
			font-size: 26rpx;
			margin-top: 5rpx;
		}
	}
}

/* 加载更多样式 */
.load-more {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 30rpx 0;
	color: #999;
	font-size: 28rpx;
	margin-top: 20rpx;
	
	.loading-dot {
		width: 16rpx;
		height: 16rpx;
		margin: 0 8rpx;
		background-color: #F26B2E;
		border-radius: 50%;
		animation: loading 1.4s infinite ease-in-out both;
	}
}

/* 没有更多数据 */
.no-more-data {
	text-align: center;
	padding: 30rpx 0;
	color: #999;
	font-size: 28rpx;
	margin-top: 20rpx;
}

@keyframes loading {
	0%, 80%, 100% { transform: scale(0); }
	40% { transform: scale(1); }
}
</style>